<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1 {
            width: 100px;
            height: 100px;
            background: red;
            display: block;
        }

        .box2 {
            width: 100px;
            height: 100px;
            background: blue;
            display: none;
        }

        .box3 {
            width: 100px;
            height: 100px;
            background: yellow;
            display: none;
        }
    </style>
</head>

<body>
    <div class="contaner1">
        <button>按钮一</button>
        <button>按钮二</button>
        <button>按钮三</button>
        <div class="box1">1</div>
        <div class="box2">2</div>
        <div class="box3">3</div>
    </div>
    <button class="next">下一页</button>

    <div class="contaner2">
        <button>按钮一</button>
        <button>按钮二</button>
        <button>按钮三</button>
        <div class="box1">1</div>
        <div class="box2">2</div>
        <div class="box3">3</div>
    </div>
    <script>
        function tab(btns, divs) {
            let obj = {}
            var num = 0
            obj.num = num
            btns.forEach((btn, key) => {
                btn.onclick = function () {
                    switchTab(key)
                    obj.num = key
                }
            })
            function switchTab(key) {
                divs.forEach((div, index) => {
                    if (key == index) {
                        div.style.display = "block"
                    } else {
                        div.style.display = "none"
                    }
                })
            }
            obj.switchTab = switchTab;
            return obj
        }
        var btns = document.querySelectorAll(".contaner1 button")
        var divs = document.querySelectorAll(".contaner1 div")
        var tab1 = tab(btns, divs, true)
        var btnnext = document.querySelector(".next")
        tab1.num++
        btnnext.onclick = function () {
            tab1.switchTab(tab1.num)
            tab1.num++
            if (tab1.num > 2) {
                tab1.num = 0
            }
        }
        var btns2 = document.querySelectorAll(".contaner2 button")
        var divs2 = document.querySelectorAll(".contaner2 div")
        var tab2 = tab(btns2, divs2)
        tab2.num++
        setInterval(() => {
            tab2.switchTab(tab2.num)
            tab2.num++
            if (tab2.num > 2) {
                tab2.num = 0
            }
        }, 1000)
    </script>

</body>

</html>